<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="dateRange" label="会议时间">
          <a-range-picker v-model:value="formState.dateRange" class="w-64" format="YYYY-MM-DD" value-format="YYYY-MM-DD" :allow-clear="false" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex justify-start">
        <a-button class="mb-2 mr-2" type="primary" @click="onAdd"> 新增 </a-button>
        <BaseExport type="战救" name="会议安排" code="000570" :search-params="formState">导出</BaseExport>
      </div>
      <BaseTable fill :data-source="tableData" row-key="id" :loading="loading" column-code="000570" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-space>
              <a-button type="link" size="small" @click="onEdit(record)">修改</a-button>
              <BaseButton confirm-text="是否作废此大事记?" type="text" danger size="small" @click="onDelRecord(record)"> 作废 </BaseButton>
            </a-space>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <AddModal v-model:open="open" :row-data="rowData" @ok="getData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import BaseButton from '@/components/BaseButton'
import { post } from '@/utils/request'
import AddModal from './AddModal'

const rowData = ref(null)
const formRef = ref(null)
const open = ref(false)
const formState = ref({
  dateRange: [dayjs(new Date().setDate(new Date().getDate() - 20)).format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})
const { loading, tableData, pagination, getData } = useTable({
  url: '/war/warMeeting/getWarMeetingList',
  immediate: true,
  params: formState.value,
  formatParams: () => {
    return {
      startDate: formState.value.dateRange[0],
      endDate: formState.value.dateRange[1]
    }
  }
})
const onEdit = (record) => {
  rowData.value = Object.assign({}, record)
  open.value = true
}
const onAdd = () => {
  rowData.value = null
  open.value = true
}
const onDelRecord = async (record) => {
  const { code, msg } = await post('/war/warMeeting/doDelWarMeeting', {
    id: record.id
  })
  if (code !== 0) return message.error(msg || '作废失败')
  getData()
  message.success('作废成功')
}
</script>
